<?php 
Class PackageModel extends Model{
	public function __construct(){
		$this->open(Model::MYSQL);
			$this->calledClass(get_class($this));
	}
	public function DisplayFront(){
	$this->open(Model::MYSQL);
		$query = "SELECT Packages.currency_id,Packages.id, Currencies.symbol, Packages.title, Packages.price, Packages.point, Packages.valid_day
				FROM Currencies INNER JOIN Packages 
				ON Currencies.id = Packages.currency_id ";
		$result = parent::query($query);
		$datas = array();
		while(($row= mysql_fetch_object($result))!=false){
			$datas[] = $row;
		}
		return $datas;
	}
	public function QueryOneRecord($where=array()){
			$this->open(Model::MYSQL);
			return parent::findAsObject($where);
	}
	public function query($query){
		$this->open(Model::MYSQL);
		$query = "SELECT * FROM Currencies";
		$result = parent::query($query) ;
		$datas = array();
		while(($row = mysql_fetch_object($result))!=false){
			$datas[] = $row ;
		}
		return $datas;
	}
	public function save($fields=array()){
		$this->open(Model::MYSQL);
		return parent::save($fields);
	}
	public function delete($where=array(),$deleteSet=false){
		$this->open(Model::MYSQL);
		return parent::delete($where,$deleteSet);
	}
	public function version($field,$where=array()){
	$this->open(Model::MYSQL);
		return parent::getFieldValue($field,$where);
	}
	public function InsertSubscription($field,$where=array()){
		$this->open(Model::MYSQL);
		return parent::getFieldValue($field,$where);
	}
	public function edit($fields=array(),$where=array()){
		$this->open(Model::MYSQL);
		return parent::update($fields,$where);
	}
	public function findObject($wheres=array()){
		$this->open(Model::MYSQL);
		return parent::findAsObject($wheres);
	}
	public function queryList(){
		$this->open(Model::MYSQL);
		$query = "SELECT * FROM Packages";
		$result = parent::query($query) ;
		$datas = array();
		while(($row = mysql_fetch_object($result))!=false){
			$datas[] = $row ;
		}
		return $datas;
	}
	public function queryJoinTable(){
			$query = "SELECT Packages.currency_id,Packages.created, Packages.id, Currencies.symbol, Packages.title, Packages.price, Packages.point, Packages.valid_day
				FROM Currencies INNER JOIN Packages 
				ON Currencies.id = Packages.currency_id ";
			return $query;
		}
		/*with WHERE word*/
	public function DisplayAdmin($query,$page=1,$perpage=20,$search = array(), $sortFields = array() , $sortCriteria = "ASC", $operation = "AND" ){
		$this->open(Model::MYSQL);
		$page=$page<1?1:$page;
		$perpage=$perpage<1?20:$perpage;
		$sortCriteria=$sortCriteria===""?"ASC":$sortCriteria;
		$operation=$operation===""?"AND":$operation;
		$query = "$query WHERE" ;
		if(isset($search)&&$search!==""){
			$tempSearchFields=array();
			foreach($search as $key=>$val){
				$tempSearchFields[]= " `$key` LIKE '%". $val ."%' " ;
			}
			$query .= implode(" $operation ",$tempSearchFields);
		}
		if(!empty($sortFields)){
			$tempSortFields=array();
			foreach($sortFields AS $field ){
				$tempSortFields[]= " `$field` " ;
			}
			$query .= " ORDER BY " . implode(" , ",$tempSortFields) . " " . $sortCriteria . " " ;
		}
		$totalRecords = mysql_num_rows(parent::query($query));
		$totalPages = ceil($totalRecords/$perpage);
		$query .= " LIMIT ".(($page-1)*$perpage)." , ".$perpage." ; " ;
		$result = parent::query($query);
		if($result!==null){
			$rows = $result ;
			$result = array();
			while(($row=mysql_fetch_object($rows))!==false)$result[]=$row;
		}
		return array("query"=>$query,"search"=>$search,"page"=>$page,"perpage"=>$perpage,"totalpages"=>$totalPages,"totalrecords"=>$totalRecords,"records"=>($result===null?array():$result)) ;
	}
	/*Multi table with find as object*/
	public function GetFieldWithMultiTable($query, $where=array()){
		$this->open(Model::MYSQL);
		if(is_array($where)&&!empty($where)){
			$query = " $query " ;
			$temp=array();
			foreach($where as $fieldName => $fieldValue ){
				$temp[] = $fieldName . "'" . mysql_real_escape_string( $fieldValue ) . "' " ;
			}
			$query .= " WHERE " . implode( " AND " , $temp ) . " ; " ;
			$result = parent::query($query);
			if($result!==null){
				$rows = $result ;
				$result = array();
				while(($row=mysql_fetch_object($rows))!==false)$result[]=$row;
			}
			return $result===null?$result:(empty($result)?null:$result[0]);
		}
	}
	
}